package com.missilike.api.controller;

import com.missilike.biz.IMilAccountService;
import com.missilike.biz.IMilContentCommentService;
import com.missilike.biz.IMilPhotoService;
import com.missilike.core.domain.CommentRequest;
import com.missilike.core.result.BaseResultCode;
import com.missilike.core.result.CommonResult;
import com.missilike.core.result.ResultCode;
import com.missilike.core.utils.ResultUtils;
import com.missilike.domain.response.MilUserFeeds;
import com.missilike.repository.entity.MilAccount;
import com.missilike.repository.entity.MilContentComment;
import com.missilike.repository.entity.extend.MilCommentExtend;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/comment")
public class CommentController {
    private final IMilContentCommentService commentService;
    private final IMilAccountService accountService;
    private final IMilPhotoService photoService;


    @Autowired
    public CommentController(IMilContentCommentService commentService, IMilAccountService accountService, IMilPhotoService photoService) {
        this.commentService = commentService;
        this.accountService = accountService;
        this.photoService = photoService;
    }
    @ApiOperation(value = "addComment", notes = "发表评论,replyCommentId 为回复的评论id,直接评论文章的replyCommentId为空")
    @RequestMapping(value = "/addComment", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public CommonResult<MilCommentExtend> addComment(@RequestBody CommentRequest commentRequest) {
        if (commentRequest == null){
            return ResultUtils.error(BaseResultCode.PARAMETER_ERROR.getCode(),"入参为null");
        }else  if (commentRequest.getAccountId() == null || commentRequest.getContentId() == null || commentRequest.getComment() == null ){
            return ResultUtils.error(BaseResultCode.PARAMETER_ERROR.getCode(),"入参属性为null");
        }
        MilContentComment comment = new MilContentComment();
        BeanUtils.copyProperties(commentRequest,comment);
        comment.setCreateTime(LocalDateTime.now());
        comment.setCommentVoice(commentRequest.getCommentVoice());
        commentService.save(comment);
        MilCommentExtend commentExtend = new MilCommentExtend();
        if(comment.getId() != null){
            MilAccount account = accountService.getById(comment.getAccountId());
            BeanUtils.copyProperties(comment,commentExtend);
            commentExtend.setAccount(account.getAccount());
            commentExtend.setFaceUrl(photoService.getPhotoUrl(account.getFaceUrlId()));
            commentExtend.setAccountNikeName(account.getNickName());
        }

        return ResultUtils.ok(commentExtend);
    }
}
